﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Quanlynhasach.hethongxuly;
using Quanlynhasach.hethongluutru;



namespace Quanlynhasach
{
    public partial class frm_PhieuBanSach : Form
    {
        public frm_PhieuBanSach()
        {
            InitializeComponent();
        }
        public void HienThiDanhSachHoaDon()
        {
            dgvDSHoaDon.DataSource = HoaDonDAO.LayDanhSachHoaDon();
        }
        public void HienThiDanhSachChiTietHoaDon()
        {
            dgvCTHoaDon.DataSource = HoaDonDAO.LayDanhSachChiTietHoaDon();
        }
     

        private void frm_PhieuBanSach_Load(object sender, EventArgs e)
        {
            thunho();
            HienThiDanhSachChiTietHoaDon();
            HienThiDanhSachHoaDon();
          
            // load du lieu cho combobox cbKhachHang 
            cbKhachHang.DataSource = KhachHangDAO.LayDanhSachKhachHang();
            cbKhachHang.DisplayMember = "TenKhachHang";
            cbKhachHang.ValueMember = "MaKhachHang";
          
           
        }
       
        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            phongto();
        }
        public void phongto()
        {
            this.Size = new Size(880,393);
        }
        public void thunho()
        {
            this.Size = new Size(431, 393);
        }
       
        private void btnThoat_Click(object sender, EventArgs e)
        {
            thunho();
        }

        public static string MaSach;
        public static string GiaBan;
        public void LayMaSach()
        {
            txtMaSach.Text = MaSach;
            txtDonGia.Text = GiaBan;
        }
        private void btnLay_Click(object sender, EventArgs e)
        {
            frm_DanhMucSach f = new frm_DanhMucSach();
            f.ShowDialog();
            LayMaSach();  
        }

        private void dgvDSHoaDon_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDSHoaDon.SelectedRows.Count > 0)
            {
                txtMaHoaDon.Text = dgvDSHoaDon.SelectedRows[0].Cells[0].Value.ToString();
                txtMaKhachHang.Text = dgvDSHoaDon.SelectedRows[0].Cells[1].Value.ToString();

            }
           
        }

        private void dgvCTHoaDon_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvCTHoaDon.SelectedRows.Count > 0)
            {
                
                txtCTHD.Text = dgvCTHoaDon.SelectedRows[0].Cells[0].Value.ToString();
                txtMaSach.Text = dgvCTHoaDon.SelectedRows[0].Cells[1].Value.ToString();
                txtSoLuong.Text = dgvCTHoaDon.SelectedRows[0].Cells[2].Value.ToString();

            }
        }

        private void btnLưu_Click(object sender, EventArgs e)
        {
            DataTable dt = QuyDinhDAO.GetQuyDinh();
            int NoToiDa =Convert.ToInt32(dt.Rows[0].ItemArray[3].ToString());
            int LuongTonMin = Convert.ToInt32(dt.Rows[0].ItemArray[4].ToString());

            DataTable dt1 = QuyDinhDAO.SlectTienNoKH(Convert.ToInt32(txtMaKhachHang.Text));
            int TienNoKH = Convert.ToInt32(dt1.Rows[0].ItemArray[5].ToString());
            HoaDonDTO hdDTO = new HoaDonDTO();
            hdDTO.MaSach = Convert.ToInt32(txtMaSach.Text);

            if (TienNoKH <= NoToiDa)
            {
                DataTable dt2 = QuyDinhDAO.SlectSoLuongTon(hdDTO.MaSach);
                int LuongTon = Convert.ToInt32(dt2.Rows[0].ItemArray[5].ToString());
                int LuongMua = Convert.ToInt32(txtSoLuong.Text);
                int TongTien = TienNoKH + (LuongMua * Convert.ToInt32(txtDonGia.Text));
                if ((LuongTon - LuongMua) < LuongTonMin)
                {
                    MessageBox.Show("Lượng Tồn Sau Khi Bán Nhỏ Hơn Quy Định");
                    return;
                }
                else
                {
                    int LuongTonMoi = LuongTon - LuongMua;
                    SachDTO sDTO = new SachDTO();
                    sDTO.MaSach = Convert.ToInt32(txtMaSach.Text);
                    sDTO.SoLuongTon = LuongTonMoi;
                    SachDAO.UpdateSoLuongTon(sDTO);
                    KhachHangDTO khDTO = new KhachHangDTO();
                    khDTO.MaKhachHang = Convert.ToInt32(txtMaKhachHang.Text);
                    khDTO.TienNo = TongTien;
                    KhachHangDAO.UpdateTienNo(khDTO);

                    BaoCaoTonDTO bctDTO = new BaoCaoTonDTO();
                    bctDTO.MaSach = sDTO.MaSach;
                    bctDTO.NgayPhatSinh = dtNgayLap.Value;
                    bctDTO.TonDau = LuongTon;
                    bctDTO.PhatSinh = "Bán sách";
                    bctDTO.TonCuoi = LuongTonMoi;
                    BaoCaoTonDAO.Insert(bctDTO);

                    BaoCaoCongNoDTO bcnDTO = new BaoCaoCongNoDTO();
                    bcnDTO.MaKhachHang = khDTO.MaKhachHang;
                    bcnDTO.NgayPhatSinh = bctDTO.NgayPhatSinh;
                    bcnDTO.NoDau = TienNoKH;
                    bcnDTO.PhatSinh = "Hóa đơn bán sách";
                    bcnDTO.NoCuoi = TongTien;
                    BaoCaoCongNoDAO.Insert(bcnDTO);

                    hdDTO.MaHoaDon=Convert.ToInt32(txtMaHoaDon.Text);
                    hdDTO.maKhachHang = Convert.ToInt32(txtMaKhachHang.Text);
                    hdDTO.MaSach = Convert.ToInt32(txtMaSach.Text);
                    hdDTO.SoLuong = Convert.ToInt32(txtSoLuong.Text);
                    hdDTO.DonGiaBan = Convert.ToInt32(txtDonGia.Text);
                    HoaDonDAO.ThemChitietHoaDon(hdDTO);
                    
                    HienThiDanhSachChiTietHoaDon();
                }

            }
            else 
            {
                MessageBox.Show("Tiền nợ quý khách quá quy định để mua sách");
            }

        }
        private void txtSoLuong_Leave(object sender, EventArgs e)
        {
            if (int.Parse(txtDonGia.Text) > 0)
            {

                UInt64 ThanhTien = UInt64.Parse(txtSoLuong.Text) * UInt64.Parse(txtDonGia.Text);

                txtThanhTien.Text = ThanhTien.ToString();
            }
            else
            {
                MessageBox.Show("Không có Sách");
                return;
            }

        }
        private void btnThêm_Click_1(object sender, EventArgs e)
        {
            HoaDonDTO hdDTO = new HoaDonDTO();
            hdDTO.maKhachHang = Convert.ToInt32(cbKhachHang.SelectedValue);
            hdDTO.NgayLap = dtNgayLap.Value;

            HoaDonDAO.ThemHoaDon(hdDTO);
            HienThiDanhSachHoaDon();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSoanLai_Click(object sender, EventArgs e)
        {
            txtCTHD.Clear();
            txtMaKhachHang.Clear();
            txtMaSach.Clear();
            txtSoLuong.Clear();
            txtDonGia.Clear();
            txtThanhTien.Clear();
        }

      
    }
}


      

       

       

